package com.example.renren.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 注意：Swagger要显示实体类，需要Controller层返回实体类
 * @author 标记
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket createRestApi() {
        /**
         * RequestHandlerSelectors,配置要扫描接口的方式
         * basePackage:指定要扫描的包
         * any():扫描全部
         * none():都不扫描
         * withClassAnnotation: 扫描类上的注解,参数是一个注解的反射对象
         * withMethodAnnotation: 扫描方法上的注解
         * paths: 过滤什么路径
         */
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.renren.controller"))
                .paths(PathSelectors.any()).build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("项目后端接口介绍")
                .termsOfServiceUrl("http://blog.csdn.net/java_yes")
                .version("1.0").build();
    }
}
